package com.ws.interceptor;

import java.util.List;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class AuthInterceptor extends AbstractPhaseInterceptor<SoapMessage> {

	public AuthInterceptor() {
		super(Phase.PRE_INVOKE);
	}

	@Override
	public void handleMessage(SoapMessage msg) throws Fault {
		System.out.println(msg);
		List<Header> headers = msg.getHeaders();
		if (headers == null || headers.size() < 1)
			throw new Fault(new IllegalArgumentException("没有header,别想调用!"));
		Header header = headers.get(0);
		Element element = (Element) header.getObject();
		NodeList userIds = element.getElementsByTagName("userId");
		if (userIds.getLength() != 1)
			throw new Fault(new IllegalArgumentException("用户名格式不对!"));
		NodeList userPasses = element.getElementsByTagName("userPass");
		if (userPasses.getLength() != 1)
			throw new Fault(new IllegalArgumentException("密码格式不对!"));
		String userId = userIds.item(0).getTextContent();
		String userPass = userPasses.item(0).getTextContent();
		if (!"admin".equals(userId) || !"123456".equals(userPass))
			throw new Fault(new IllegalArgumentException("没有权限!"));

	}

}
